package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.view.wheelview.NumericWheelAdapter;
import com.enqualcomm.kids.view.wheelview.WheelView;

import common.utils.DensityUtil;

/**
 * Created by Administrator on 2015/9/10.
 */

public class MyToolsTimerPickerDialog2 extends Dialog implements
        View.OnClickListener{
    private final OnDateTimeSetListener mCallBack;
    //	private final Calendar mCalendar;
    private int curr_year, curr_month, curr_day, curr_hour, curr_minute;

    final WheelView wv_hours, wv_mins;
    private int windowWidth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width =windowWidth - DensityUtil.dip2px(getContext(), 30); // 宽度
        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }
    public MyToolsTimerPickerDialog2(Context context,OnDateTimeSetListener callBack,int hour,int minute) {
        super(context);
        mCallBack = callBack;

        // 找到dialog的布局文件
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.tool_time_layout, null);

        view.findViewById(R.id.sureBtn).setOnClickListener(this);
        view.findViewById(R.id.cancelBtn).setOnClickListener(this);

        TextView title = (TextView) view.findViewById(R.id.titleText);
        title.setText(getContext().getString(R.string.settime));

        DisplayMetrics dm = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        windowWidth = dm.widthPixels;
        int textSize = DensityUtil.adjustFontSize(windowWidth);
        // 时
        wv_hours = (WheelView) view.findViewById(R.id.hour);
        wv_hours.setAdapter(new NumericWheelAdapter(0, 23));// 设置"时"的显示数据
        wv_hours.setCyclic(true);// 可循环滚动
        wv_hours.setLabel(context.getString(R.string.hour));// 添加文字
        wv_hours.setCurrentItem(hour);// 初始化时显示的数据
        wv_hours.setTextSizeAndItemCount(textSize, 3);

        // 分
        wv_mins= (WheelView) view.findViewById(R.id.minute);
        wv_mins.setAdapter(new NumericWheelAdapter(0,60));
        wv_mins.setCyclic(true);
        wv_mins.setLabel(context.getString(R.string.minute));
        wv_mins.setCurrentItem(minute);
        wv_mins.setTextSizeAndItemCount(textSize, 3);


        setContentView(view);
    }
    public void onClick(View v) {
        if (v.getId() == R.id.cancelBtn) {
            dismiss();
        }else {
            curr_hour = wv_hours.getCurrentItem();
            curr_minute = wv_mins.getCurrentItem();
            if (mCallBack != null) {
                mCallBack.onDateTimeSet(curr_year, curr_month, curr_day, curr_hour,curr_minute);
            }
            dismiss();
        }

    }
    public  interface OnDateTimeSetListener {
        void onDateTimeSet(int year, int monthOfYear, int dayOfMonth, int hour,
                           int minute);
    }

}


